package com.bringspring.system.permission.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bringspring.common.util.*;
import com.bringspring.system.base.model.link.WeChatDepartment;
import com.bringspring.system.message.util.SynQyWebChatUtil;
import com.bringspring.system.permission.entity.QyLinkUserEntity;
import com.bringspring.system.permission.entity.UserEntity;
import com.bringspring.system.permission.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class WeChatUserThread implements Callable<List<UserEntity>> {

    @Autowired
    private UserProvider userProvider;
    @Autowired
    private UserService userService;
    private List<WeChatDepartment> jsonToList;
    private String access_token = "";
    private WeChatDepartment weChatDepartment = new WeChatDepartment();
    private String listByEnCode = "";

    public WeChatUserThread(List<WeChatDepartment> jsonToList, UserProvider userProvider, String access_token, String listByEnCode, UserServiceImpl userServiceImpl) {
        this.jsonToList = jsonToList;
        this.userProvider = userProvider;
        this.access_token = access_token;
        this.listByEnCode = listByEnCode;
        this.userService = userServiceImpl;
    }

    @Override
    public List<UserEntity> call() throws Exception {
        List<UserEntity> jsonToListMap = new ArrayList<>();
        for (WeChatDepartment o : jsonToList) {
            JSONObject userList = SynQyWebChatUtil.getLinkedcorpUser(access_token, "wh87896fbeb5dcdd15/" + o.getDepartment_id());
            JSONObject jo = JSONObject.parseObject(String.valueOf(userList));
            String userlist = String.valueOf(jo.get("userlist"));
            List<Map<String, Object>> jsonToListMap1 = JsonUtil.getJsonToListMap(userlist);
            for (Map map : jsonToListMap1) {
                UserEntity userEntity = new UserEntity();
                QyLinkUserEntity jsonToBean = JsonUtil.getJsonToBean(map, new QyLinkUserEntity().getClass());
                // 判断是不是为空,需要挨个判断
                // 去掉除汉字以外的字符
                if (!StringUtils.isNull(jsonToBean.getName())) {
                    userEntity.setRealName(jsonToBean.getName().replaceAll("[^\u4e00-\u9fa5]", ""));
                }
                // 账户用userID id
                if (!StringUtils.isNull(jsonToBean.getUserid())) {
                    userEntity.setId(String.valueOf(jsonToBean.getUserid()));
                    userEntity.setAccount(String.valueOf(map.get("userid")));
                }
                // 拼接处理department
                if (!StringUtils.isNull(jsonToBean.getDepartment())) {
                    List department = jsonToBean.getDepartment();
                    String departmentStr = "";
                    for (Object depart : department) {
                        String s1 = String.valueOf(depart);
                        String[] split1 = s1.split("/");
                        String s3 = split1[split1.length - 1];
                        String s2 = s1.replaceAll("\\[", "").replaceAll("]", "").replaceAll("\"", "");
                        departmentStr += s3 + ",";
                    }
                    departmentStr = departmentStr.substring(0, departmentStr.length() - 1);
                    userEntity.setOrganizeId(departmentStr);
                }
                if (!StringUtils.isNull(jsonToBean.getMobile())) {
                    userEntity.setMobilePhone(jsonToBean.getMobile());
                }
                if (!StringUtils.isNull(jsonToBean.getTelephone())) {
                    userEntity.setMobilePhone(jsonToBean.getTelephone());
                }
                if (!StringUtils.isNull(jsonToBean.getEmail())) {
                    userEntity.setEmail(jsonToBean.getEmail());
                }
                if (!StringUtils.isNull(jsonToBean.getPosition())) {
                    userEntity.setPositionId(jsonToBean.getPosition());
                }
                //获取头像  微信企业没传，暂定为默认
                userEntity.setHeadIcon("/api/file/Image/userAvatar/001.png");
                // 性别默认为3
                userEntity.setGender(3);
                userEntity.setEnabledMark(1);
                //添加用户 初始化
                userEntity.setSecretkey(RandomUtil.uuId());
                userEntity.setPassword(userService.getDefaultPassword(userEntity.getSecretkey()));
                userEntity.setPassword(Md5Util.getStringMd5(userEntity.getPassword().toLowerCase() + userEntity.getSecretkey().toLowerCase()));
                userEntity.setIsAdministrator(0);
                userEntity.setCreatorUserId(userProvider.get().getUserId());

                userEntity.setRoleId(listByEnCode);
                userService.saveOrUpdateCommon(userEntity.getId(), userEntity);
                jsonToListMap.add(userEntity);
            }
        }
        return jsonToListMap;
    }

}
